<template>
  <div class="loading-container">
    <div class="loading-content">
      <div class="logo-container">
        <div class="circle-outer"></div>
        <div class="circle-middle"></div>
        <div class="circle-inner"></div>
        <div class="ai-icon">面试通</div>
      </div>

      <h1 class="loading-title">智能面试助手</h1>
      <div class="typing-container">
        <p class="typing-text">
          正在准备您的面试题目<span class="dots"><span>.</span><span>.</span><span>.</span></span>
        </p>
      </div>

      <div class="progress-container">
        <div class="progress-bar" :style="{ width: `${progress}%` }"></div>
        <div class="progress-percentage">{{ progress }}%</div>
      </div>
    </div>

    <div class="particles-container">
      <div
        v-for="n in 20"
        :key="n"
        class="particle"
        :style="{
          '--size': `${Math.random() * 10 + 5}px`,
          '--left': `${Math.random() * 100}%`,
          '--delay': `${Math.random() * 5}s`,
          '--duration': `${Math.random() * 10 + 10}s`,
        }"
      ></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import gsap from 'gsap'

const router = useRouter()
const progress = ref(0)
const intervalId = ref<number | null>(null)

onMounted(() => {
  // 模拟5秒加载进度
  intervalId.value = window.setInterval(() => {
    progress.value += 1
    if (progress.value >= 100) {
      clearInterval(intervalId.value as number)
      // 稍微延迟跳转，让用户看到100%完成
    }
  }, 3000) // 5s * 100 = 500秒完成

  nextTick(() => {
    gsap.from('.loading-content', { opacity: 0, y: 40, duration: 1, ease: 'power3.out' })
    gsap.from('.logo-container', {
      opacity: 0,
      scale: 0.9,
      duration: 1,
      delay: 0.2,
      ease: 'elastic.out(1, 0.5)',
    })
    gsap.from('.loading-title', { opacity: 0, y: -20, duration: 0.7, delay: 0.4 })
    gsap.from('.typing-container', { opacity: 0, y: 10, duration: 0.7, delay: 0.6 })
    gsap.from('.progress-container', { opacity: 0, scaleX: 0.95, duration: 0.7, delay: 0.8 })
    gsap.from('.particles-container .particle', {
      opacity: 0,
      y: 20,
      duration: 1,
      delay: 0.5,
      stagger: 0.05,
    })
  })
})

onUnmounted(() => {
  if (intervalId.value) {
    clearInterval(intervalId.value)
  }
})
</script>

<style scoped>
.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #f5f7fa, #e4e8f0);
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9999;
  font-family: 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
}

.loading-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 600px;
  text-align: center;
  z-index: 2;
}

/* 炫酷Logo设计 */
.logo-container {
  position: relative;
  width: 150px;
  height: 150px;
  margin-bottom: 2rem;
  display: flex;
  justify-content: center;
  align-items: center;
}

.circle-outer {
  position: absolute;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  border: 2px solid rgba(64, 158, 255, 0.5);
  animation: rotate 10s linear infinite, pulse 2s ease-in-out infinite alternate;
}

.circle-middle {
  position: absolute;
  width: 120px;
  height: 120px;
  border-radius: 50%;
  border: 3px solid rgba(82, 145, 237, 0.7);
  animation: rotate 7s linear infinite reverse, pulse 3s ease-in-out infinite alternate;
}

.circle-inner {
  position: absolute;
  width: 90px;
  height: 90px;
  border-radius: 50%;
  background: rgba(24, 144, 255, 0.2);
  box-shadow: 0 0 20px rgba(24, 144, 255, 0.4);
  animation: pulse 2s ease-in-out infinite;
}

.ai-icon {
  font-size: 2.5rem;
  font-weight: bold;
  color: #1890ff;
  z-index: 10;
  text-shadow: 0 0 10px rgba(24, 144, 255, 0.5);
  animation: glow 2s ease-in-out infinite alternate;
}

.loading-title {
  font-size: 2.8rem;
  margin-bottom: 0.5rem;
  background: linear-gradient(to right, #1890ff, #096dd9);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  font-weight: bold;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  letter-spacing: 1px;
}

/* 打字效果 */
.typing-container {
  height: 1.5rem;
  margin: 1rem 0 2rem;
}

.typing-text {
  font-size: 1.2rem;
  color: #555;
  font-weight: 500;
}

.dots span {
  opacity: 0;
  animation: dot 1.5s infinite;
  animation-fill-mode: both;
}

.dots span:nth-child(2) {
  animation-delay: 0.5s;
}

.dots span:nth-child(3) {
  animation-delay: 1s;
}

.progress-container {
  width: 85%;
  height: 8px;
  background-color: rgba(0, 0, 0, 0.05);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  position: relative;
  margin-bottom: 2.5rem;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, #1890ff, #096dd9);
  border-radius: 10px;
  transition: width 0.2s ease;
  box-shadow: 0 0 15px rgba(24, 144, 255, 0.5);
  position: relative;
  overflow: hidden;
}

.progress-bar::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    90deg,
    rgba(255, 255, 255, 0) 0%,
    rgba(255, 255, 255, 0.4) 50%,
    rgba(255, 255, 255, 0) 100%
  );
  animation: shine 2s infinite;
}

.progress-percentage {
  position: absolute;
  top: 15px;
  left: 50%;
  transform: translateX(-50%);
  color: #555;
  font-size: 0.8rem;
  font-weight: 500;
}

/* 粒子效果背景 */
.particles-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.particle {
  position: absolute;
  width: var(--size);
  height: var(--size);
  background-color: rgba(24, 144, 255, 0.15);
  border-radius: 50%;
  bottom: -100px;
  left: var(--left);
  animation: float-up var(--duration) infinite;
  animation-delay: var(--delay);
}

/* 动画定义 */
@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.05);
    opacity: 0.8;
  }
}

@keyframes glow {
  from {
    text-shadow: 0 0 5px #fff, 0 0 10px #1890ff;
  }
  to {
    text-shadow: 0 0 10px #fff, 0 0 20px #1890ff, 0 0 30px #1890ff;
  }
}

@keyframes dot {
  0% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

@keyframes shine {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

@keyframes float-up {
  0% {
    transform: translateY(0) scale(1);
    opacity: 0;
  }
  10% {
    opacity: 0.8;
  }
  90% {
    opacity: 0.5;
  }
  100% {
    transform: translateY(-1200px) scale(0.2);
    opacity: 0;
  }
}
</style>
